﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ControleRepresentante.Logic.ImpostoHandlers;
using ControleRepresentante.Logic.Repositories;
using WebSite.Controllers.ControllerExtensions;
using WebSite.Controllers.PrintTemplates;

namespace WebSite.Controllers
{
    public class SearchController : Controller
    {
        private readonly IRepositoryProvider m_RepositoryProvider;

        public SearchController(IRepositoryProvider repositoryProvider, ISessionConfiguration sessionConfiguration, IControllerExtensionProvider controllerExtensionProvider, IPrintTemplateSelector printTemplateSelector, IImpostoProvider impostoProvider)
        {
            m_RepositoryProvider = repositoryProvider;
        }

        [HttpPost]
        public ActionResult SearchCliente(string text)
        {
            IClienteRepository clienteRepository = m_RepositoryProvider.GetRepository<IClienteRepository>();
            Dictionary<Guid, string> clientes = clienteRepository.GetModelNames(text);
            return new JsonResult { Data = clientes.Values };
        }

        public ActionResult SearchClienteToken(string q)
        {
            IClienteRepository clienteRepository = m_RepositoryProvider.GetRepository<IClienteRepository>();
            var result = clienteRepository.GetModelNames(q)
                .Select(x => new {id = x.Key, name = x.Value});
            return new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public ActionResult SearchVendedor(string q)
        {
            IVendedorRepository vendedorRepository = m_RepositoryProvider.GetRepository<IVendedorRepository>();
            var result = vendedorRepository.GetModelNames(q)
                .Select(x => new { id = x.Key, name = x.Value });
            return new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public ActionResult SearchFabrica(string q)
        {
            IFabricaRepository fabricaRepository = m_RepositoryProvider.GetRepository<IFabricaRepository>();
            var result = fabricaRepository.GetModelNames(q)
                .Select(x => new { id = x.Key, name = x.Value });
            return new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public ActionResult SearchTransportadora(string q)
        {
            ITransportadoraRepository transportadoraRepository = m_RepositoryProvider.GetRepository<ITransportadoraRepository>();
            var result = transportadoraRepository.GetModelNames(q)
                .Select(x => new { id = x.Key, name = x.Value });
            return new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }
}
